﻿using EmbeddedSharp.Core;

namespace Zove.Runtime.Core
{
    /// <summary>
    /// Solution formulator
    /// </summary>
    public class SolutionFormulator
    {
        public string Formulate(RuntimeFactor rf)
        {
            if (rf.Status == RuntimeFactorStatus.Input)
            {
                return rf.QualifiedName; // rf.Name;
            }
            if (rf.Status == RuntimeFactorStatus.Unknown)
            {
                return "<n/a>";
            }

            var eci = new DefaultExpressionTranslator();
            string result = eci.Translate(rf.SolvedRule.Root);
            foreach (RuntimeFactor rff in rf.SolvedRule.Equation.Factors)
            {
                if (rff.Name == rf.Name)
                {
                    continue;
                }

                string modified = Formulate(rff);
                result = Utils.ReplaceString(
                    rff.Name, modified, result);
            }

            return result;
        }
    }
}